package com.fingard.dsp.bank.directbank.lzcb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.directbank.hrrz01.HRRZ01Header;
import com.fingard.dsp.bank.directbank.lzcb01.LZCB01Base;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class LZCB01Transaction extends LZCB01Base implements ITransaction {
    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        //返回ATS的对象
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            LZCB01Header header = new LZCB01Header();
            header.transCode = "B2ECSTZActTrsQry";
            header.reqSeqNo = p_req7516.reqHeader.transSeqID;
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());
            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7516.getDateTo());
            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            while (tmpIntDateFrom <= tmpIntDateTo && StringHelper.isNullOrEmpty(tmpRespCode)) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                if (DateHelper.compareMonth(tmpCalendarFrom, tmpCalendarTo) == -1) {
                    tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
                } else {
                    tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
                }
                    XmlTextWriter xmlWriter = new XmlTextWriter();//发送银行报文的XML对象
                    //写入通用的头部节点
                    writeCommonHeader(xmlWriter, header);
                    xmlWriter.writeStartElement("Body");
                    xmlWriter.writeElementString("AcNo", p_req7516.actNum);
                    xmlWriter.writeElementString("BeginDate", Format.Date8Format.format(tmpCalendarFrom.getTime()));
                    xmlWriter.writeElementString("EndDate", Format.Date8Format.format(tmpClndrStepTo.getTime()));
                    xmlWriter.writeEndDocument();
                    String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),header);
                    if (tmpStrRet[0].length() == 0) {
                        StringBuilder tmpSbMsg = new StringBuilder();
                        XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                        if (tmpBkReader != null) {
                            String tmpRetCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                            String tmpRetInfo = tmpBkReader.getFirstText("Message/Head/IBSReturnMsg");
                            if ("000000".equals(tmpRetCode)) {
                                String repCode = tmpBkReader.getFirstText("/Message/Body/ReturnCode");
                                String repInfo = tmpBkReader.getFirstText("Message/Body/ReturnMsg");
                                if(repCode!=null && repCode.equals("000000")) {
                                        while (tmpBkReader.read("/Message/Body/List/Map")) {
                                            RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                            tmpRetDetail.actNum = p_req7516.actNum;
                                            tmpRetDetail.recAccNum = tmpBkReader.readedXElement.getSingleText("AcctNo2");
                                            tmpRetDetail.recAccName = tmpBkReader.readedXElement.getSingleText("PayeeAcName");
                                            tmpRetDetail.recAccBank = tmpBkReader.readedXElement.getSingleText("PayeeBankName");
                                            String tmpTranDate = tmpBkReader.readedXElement.getSingleText("TransDate");
                                            if(tmpTranDate.length()==8 ){
                                                tmpTranDate = tmpTranDate.substring(0,4) + "-" + tmpTranDate.substring(4,6) + "-" + tmpTranDate.substring(6,8);
                                            }
                                            tmpRetDetail.tranDate = tmpTranDate;
                                            String tmpTranTime = tmpBkReader.readedXElement.getSingleText("TransTime");//交易时间戳
                                            if(StringHelper.isNullOrEmpty(tmpTranTime)){
                                                tmpTranTime =tmpTranDate + " 00:00:00";
                                            }
                                            tmpRetDetail.tranTime = tmpTranTime;
                                            tmpRetDetail.valueDate = tmpTranDate;//起息日没有返回就用交易日
                                            tmpRetDetail.accBal = tmpBkReader.readedXElement.getSingleText("Balance");

                                            String tmpTranAmt = tmpBkReader.readedXElement.getSingleText("Amount");
                                            String tmpDCFlag = tmpBkReader.readedXElement.getSingleText("DCFlag");
                                            tmpDCFlag = "0".equals(tmpDCFlag) ? "1" : ("1".equals(tmpDCFlag) ? "2" : "");
                                            tmpRetDetail.tranAmt = tmpTranAmt;
                                            tmpRetDetail.cdFlag = tmpDCFlag;
                                            String tmpPurpose = tmpBkReader.readedXElement.getSingleText("Remark");
                                            String[] tmpArr = tmpPurpose.split("-");
                                            if (tmpArr.length >= 2) {
                                                tmpRetDetail.usage = tmpArr[0];
                                                tmpRetDetail.abstr = tmpArr[1];
                                            } else {
                                                tmpRetDetail.usage = tmpPurpose;
                                            }
                                            tmpRetDetail.cur = p_req7516.actCur;
                                            retAts.writeRetDetail(tmpRetDetail);
                                        }
                                    } else {
                                        tmpRespCode = "-1";
                                        tmpRespInfo = repInfo;
                                        break;
                                    }
                                }else{
                                    tmpRespCode = "-1";
                                    tmpRespInfo = tmpRetInfo;
                                    break;
                                }

                            } else {
                                tmpRespCode = "-1";
                                tmpRespInfo = tmpSbMsg.toString();
                                break;
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpStrRet[1];
                            break;
                        }
                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(tmpRespCode)) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        Req7516HisTrans tmpReq7516 = new Req7516HisTrans(p_req7515.reqHeader);
        tmpReq7516.actArea = p_req7515.actArea;
        tmpReq7516.actCur = p_req7515.actCur;
        tmpReq7516.actNum = p_req7515.actNum;
        String tmpStrToday = Format.Date8Format.format(new Date());
        tmpReq7516.dateFrom = tmpStrToday;
        tmpReq7516.dateTo = tmpStrToday;
        return d_7516_HisTrans(tmpReq7516);
    }
}
